package com.hwhy.systemmonitoring.service.Impl;

import com.hwhy.systemmonitoring.common.util.ArithUtil;
import com.hwhy.systemmonitoring.common.util.StringUtil;
import com.hwhy.systemmonitoring.common.util.SystemInfoUtil;
import com.hwhy.systemmonitoring.entity.*;
import com.hwhy.systemmonitoring.service.IStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 实现层
 * @author LIFULIN
 * @className StatusServiceImpl
 * @description TODO
 * @date 2020/4/28-18:32
 */
@Slf4j
@Service
public class StatusServiceImpl implements IStatusService {

    /**
     * 运行状态监控页面功能（主要包含CPU、内存、磁盘状态、磁盘阵列、机房状态监控）
     *
     * @return LinkedHashMap<String, Object>
     * @author lfl
     */
    @Override
    public LinkedHashMap<String, Object> getAllMessages() {
        LinkedHashMap<String, Object> resultmap = new LinkedHashMap<>();
        SystemInfo si = new SystemInfo();
        HardwareAbstractionLayer hal = si.getHardware();

        //获取服务器信息
        Sys sys = SystemInfoUtil.setSysInfo();

        //获取cpu信息
        Cpu cpu = SystemInfoUtil.setCpuInfo(hal.getProcessor());

        //获取内存信息
        Mem mem = SystemInfoUtil.setMemInfo(hal.getMemory());

        //获取jvm信息
        Jvm jvm = SystemInfoUtil.setJvmInfo();

        //获取磁盘信息
        LinkedList<SysFile> sysFileList = SystemInfoUtil.setSysFiles(si.getOperatingSystem());

        resultmap.put("Sys", sys);
        resultmap.put("Cpu", cpu);
        resultmap.put("Mem", mem);
        resultmap.put("Jvm", jvm);
        resultmap.put("SysFileList", sysFileList);
        return resultmap;
    }

    /**
     * 获取所有监控使用率(cpu使用率，内存使用率，磁盘使用情况)
     *
     * @return
     */
    @Override
    public LinkedHashMap<Object, Object> getAllUsageRate() {
        //返回数组
        LinkedHashMap<Object, Object> allMessageMap = new LinkedHashMap<>();
        double cpu = SystemInfoUtil.getCpuUsage();
        double mem = SystemInfoUtil.getMemUsage();
        // 获取磁盘信息
        LinkedList<SysFile> sysFileList = SystemInfoUtil.setSysFiles(new SystemInfo().getOperatingSystem());
        double total = 0;
        double free = 0;
        for (SysFile sysFile : sysFileList) {
            double s = StringUtil.getNumeric(sysFile.getTotal());
            total += s;
            double s1 = StringUtil.getNumeric(sysFile.getFree());
            free += s1;
        }
        double diskUsage = ArithUtil.mul(ArithUtil.div(ArithUtil.sub(total, free), total, 2), 100);
        allMessageMap.put("Cpu", cpu);
        allMessageMap.put("Mem", mem);
        allMessageMap.put("Disk", diskUsage);
        return allMessageMap;
    }

    /**
     * 获取服务器Sys信息
     *
     * @return
     */
    @Override
    public Sys getSysMessages() {
        //获取服务器信息
        return SystemInfoUtil.setSysInfo();
    }

    /**
     * 获取Cpu信息
     *
     * @return
     */
    @Override
    public Cpu getCpuMessages() {
        SystemInfo si = new SystemInfo();
        HardwareAbstractionLayer hal = si.getHardware();
        //获取cpu信息
        return SystemInfoUtil.setCpuInfo(hal.getProcessor());
    }

    /**
     * 获取内存信息
     *
     * @return
     */
    @Override
    public Mem getMemMessages() {
        SystemInfo si = new SystemInfo();
        HardwareAbstractionLayer hal = si.getHardware();
        //获取内存信息
        return SystemInfoUtil.setMemInfo(hal.getMemory());
    }

    /**
     * 获取Jvm信息
     *
     * @return
     */
    @Override
    public Jvm getJvmMessages() {
        //获取jvm信息
        return SystemInfoUtil.setJvmInfo();
    }

    /**
     * 获取磁盘信息
     *
     * @return
     */
    @Override
    public List<SysFile> getSysFileMessages() {
        SystemInfo si = new SystemInfo();
        //获取磁盘信息
        return SystemInfoUtil.setSysFiles(si.getOperatingSystem());
    }

}

